package main.java.game.item;

import main.java.game.Player;
import main.java.game.UserInterface;
import main.java.game.combatant.Hero;
import main.java.game.combatant.HeroTwoWeapons;


public class Weapon extends AffordableEquipable{

	public Weapon(String name, Integer attackValue, Integer initValue, Integer buyingPrice, WeaponType type){
		
		super(name, 0,attackValue,initValue,buyingPrice);
		this.setType(type);
		
	}
	
	/**
	 * @uml.property  name="player"
	 * @uml.associationEnd  
	 */
	private Player player;
	private WeaponType type;

	/**
	 * Getter of the property <tt>player</tt>
	 * @return  Returns the player.
	 * @uml.property  name="player"
	 */
	public Player getPlayer() {
		return player;
	}

	/**
	 * Setter of the property <tt>player</tt>
	 * @param player  The player to set.
	 * @uml.property  name="player"
	 */
	public void setPlayer(Player player) {
		this.player = player;
	}
	
	public void equip(Hero h){
		if(h instanceof HeroTwoWeapons){
			HeroTwoWeapons hero=(HeroTwoWeapons)h;
			if(hero.getWeapon()==null && hero.getWeapon2()==null)
				equipFirstSlot(hero);
			else if(hero.getWeapon()!=null && hero.getWeapon2()==null)
				equipSecondSlot(hero);
			else if(hero.getWeapon()==null && hero.getWeapon2()!=null)
				equipFirstSlot(hero);
			else{
				UserInterface.EquipWeaponInHeroTwoWeapons(hero, this);
			}
		}
		
		else{
			equipFirstSlot(h);
		}
		
	}

	public void equipFirstSlot(Hero h) {
			if(h.getWeapon()!=null)
				this.unequip(h);
			Player p=h.getPlayer();
			if(h.getName().equals("Balmir the Brave") && this.getType()==WeaponType.SWORD){
				h.setInit(h.getInit()-(getInitValue()+1));
				h.setAttack(h.getAttack()-(getAttackValue()+1));
				h.setDefense(h.getDefense()+getDefValue());
			}
			else if(h.getName().equals("Isengrimm the Dwarf") && (this.getType()== WeaponType.AXE)){
				h.setAttack(h.getAttack()-(getAttackValue()+2));
				h.setInit(h.getInit()+getInitValue());
				h.setDefense(h.getDefense()+getDefValue());
			}
			else{
				h.setInit(h.getInit()+getInitValue());
				h.setAttack(h.getAttack()+getAttackValue());
				h.setDefense(h.getDefense()+getDefValue());
			}
			h.setWeapon(this);
			p.removeWeapon(this);
		}

	public void equipSecondSlot(HeroTwoWeapons h) {
		if(h.getWeapon()!=null)
			this.unequip(h);
		Player p=h.getPlayer();
		h.setInit(h.getInit()+getInitValue());
		h.setAttack(h.getAttack()+getAttackValue());
		h.setDefense(h.getDefense()+getDefValue());
		h.setWeapon2(this);
		p.removeWeapon(this);
	}


	@Override
	public void unequip(Hero h) {
		Weapon w=h.getWeapon();
		Player p=h.getPlayer();
		p.addWeapon(w);
		if(h.getName().equals("Balmir the Brave") && this.getType()==WeaponType.SWORD){
			h.setInit(h.getInit()-(getInitValue()-1));
			h.setAttack(h.getAttack()-(getAttackValue()-1));
			h.setDefense(h.getDefense()-getDefValue());
		}
		else if(h.getName().equals("Isengrimm the Dwarf") && (this.getType()== WeaponType.AXE)){
			h.setAttack(h.getAttack()-(getAttackValue()-2));
			h.setInit(h.getInit()-getInitValue());
			h.setDefense(h.getDefense()-getDefValue());
		}
		else{
			h.setInit(h.getInit()-getInitValue());
			h.setAttack(h.getAttack()-getAttackValue());
			h.setDefense(h.getDefense()-getDefValue());

		}
		h.setWeapon(null);
	}
	public void unequipSecondSlot(HeroTwoWeapons h) {
		Weapon w=h.getWeapon2();
		Player p=h.getPlayer();
		p.addWeapon(w);
		h.setInit(h.getInit()-getInitValue());
		h.setAttack(h.getAttack()-getAttackValue());
		h.setDefense(h.getDefense()-getDefValue());
		h.setWeapon2(null);
	}
	

	public void setType(WeaponType type) {
		this.type = type;
	}

	public WeaponType getType() {
		return type;
	}


}
